##################################################
## Replication materials for 
## "Competent legislators or mere pawns? 
## Experimental evidence of attitudes toward gender quota politicians"
## by Carolyn Barnett, Alexandra Blackman, and Marwa Shalaby

## 8. Appendix: Exploratory Analyses
## This file creates Appendix Figures A.15 to A.20 and Appendix Tables A.16 to A.18

## NOTE: Run file "01_setup.R" first to load necessary packages and data

# Additional Set-Up ---------------------------

# Transform dataset to long format 
morvign_long <- gather(morvign, measure, value, 
                       c(vignette_competence:vignette_pawnlike), 
                       factor_key=TRUE)

# Recode variables
morvign_long$vignette_election <- factor(morvign_long$vignette_election)
morvign_long$vignette_mode <- factor(morvign_long$vignette_mode)
morvign_long$vignette_work <- factor(morvign_long$vignette_work)
morvign_long$measure <- factor(morvign_long$measure, levels= c("vignette_competence", 
                                                               "vignette_pawnlike"))

# Create just female politicians dataset
morvign_fempol <- morvign %>% filter(vignette_gender == "Woman")

# Create just quota women dataset
morvign_wq <- morvign %>% filter(vignette_election=="Woman/quota")


# TRUST IN ELECTIONS --------------

## FIGURE A.15 -----------

vign_election_trust <- summarySE(morvign_long, 
                                 measurevar="value", 
                                 groupvars=c("measure","vignette_election",
                                             "trust_in_elections"), na.rm= T)

vign_election_trust <- vign_election_trust %>% 
  filter(!is.na(trust_in_elections)) 

vign_election_trust

##### CREATE GRAPH

ggplot(vign_election_trust, aes(x=trust_in_elections, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Trust in Elections", x = "Level of Trust in Elections", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_elec_trust.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)


###################################################
#       Difference in Differences                 #
#       vignette_mode * Trust in Elections        #
###################################################

m_trust_com1 <- lm(vignette_competence ~ vignette_mode * trust_in_elections, data=morvign)
summary(m_trust_com1)

m_trust_pawn1 <- lm(vignette_pawnlike ~ vignette_mode * trust_in_elections, data=morvign)
summary(m_trust_pawn1)

## TABLE A.16 ------------

# create tidy table with t-tests for variables of interest
morvign_wq_trust_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ trust_in_elections, morvign_wq)),
                                      tidy(t.test(vignette_pawnlike ~ trust_in_elections, morvign_wq))
)

# prepare table for latex estimate1 = High Trust & estimate2 = Low Trust
morvign_wq_trust_t_tests <- morvign_wq_trust_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Outcome" = variable,
                "Mean (High Trust)" = estimate1,
                "Mean (Low Trust)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

stargazer(morvign_wq_trust_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)


## FIGURE A.16 --------------

high_trust <- morvign %>% 
  filter(trust_in_elections == "High")

# set seed for reproducibility of jittered plots
set.seed(20482)

ggplot(high_trust, aes(x = vignette_competence, 
                       y = vignette_pawnlike)) +
  geom_jitter() +
  facet_wrap(~vignette_election) +
  geom_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Competence",
       y = "Pawn-Like")

ggsave("vign_corr_comp_pawn_high_trust.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 4.7, units = "in", dpi =300)


# GOVERNMENT SATISFACTION ------------------

## FIGURE A.17 -----------

vign_election_govsatis <- summarySE(morvign_long, measurevar="value", 
                                    groupvars=c("measure","vignette_election","govt_satisfaction"), na.rm= T)

vign_election_govsatis <- vign_election_govsatis %>% 
  filter(!is.na(govt_satisfaction))

vign_election_govsatis

##### CREATE GRAPH

ggplot(vign_election_govsatis, aes(x=govt_satisfaction, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Government Satisfaction", x = "Level of Government Satisfaction", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_govsatis.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)

###################################################
#       Difference in Differences                 #
#       vignette_mode * Govt satisfaction         #
###################################################

m_govsatis_com1 <- lm(vignette_competence ~ vignette_mode * govt_satisfaction, data=morvign)
summary(m_govsatis_com1)

m_govsatis_pawn1 <- lm(vignette_pawnlike ~ vignette_mode * govt_satisfaction, data=morvign)
summary(m_govsatis_pawn1)

## TABLE A.17 ------------

# create tidy table with t-tests for variables of interest
morvign_wq_govsatis_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ govt_satisfaction, morvign_wq)),
                                         tidy(t.test(vignette_pawnlike ~ govt_satisfaction, morvign_wq))
)

# prepare table for latex estimate1 = High Satis & estimate2 = Low Satis
morvign_wq_govsatis_t_tests <- morvign_wq_govsatis_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Outcome" = variable,
                "Mean (High Satis)" = estimate1,
                "Mean (Low Satis)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

stargazer(morvign_wq_govsatis_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)


## FIGURE A.18 --------------------

high_satis <- morvign %>% 
  filter(govt_satisfaction == "High")

# set seed for reproducibility of jittered plots
set.seed(20482)

ggplot(high_satis, aes(x = vignette_competence, 
                       y = vignette_pawnlike)) +
  geom_jitter() +
  facet_wrap(~vignette_election) +
  geom_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Competence",
       y = "Pawn-Like")

ggsave("vign_corr_comp_pawn_high_satis.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 4.7, units = "in", dpi =300)


# VOTING ------------

## FIGURE A.19 -----------

vign_election_voter <- summarySE(morvign_long, 
                                 measurevar="value", 
                                 groupvars=c("measure","vignette_election","Voted"), na.rm= T)

vign_election_voter <- vign_election_voter %>% filter(!is.na(Voted))

vign_election_voter

##### CREATE GRAPH

ggplot(vign_election_voter, aes(x=Voted, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Voter History", x = "Voter History", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_voted.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)

###################################################
#       Difference in Differences                 #
#       vignette_mode * Voted                     #
###################################################

m_voted_com1 <- lm(vignette_competence ~ vignette_mode * Voted, data=morvign)
summary(m_voted_com1)

m_voted_pawn1 <- lm(vignette_pawnlike ~ vignette_mode * Voted, data=morvign)
summary(m_voted_pawn1)


## TABLE A.18 ------------

# create tidy table with t-tests for variables of interest
morvign_wq_voted_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ Voted, morvign_wq)),
                                      tidy(t.test(vignette_pawnlike ~ Voted, morvign_wq))
)

# prepare table for latex estimate1 = non-voter & estimate2 = voter
morvign_wq_voted_t_tests <- morvign_wq_voted_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Outcome" = variable,
                "Mean (Non-Voters)" = estimate1,
                "Mean (Voters)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

stargazer(morvign_wq_voted_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)



## FIGURE A.20 ---------------------

voters <- morvign %>% 
  filter(Voted == "Voter")

# set seed for reproducibility of jittered plots
set.seed(20482)

ggplot(voters, aes(x = vignette_competence, 
                   y = vignette_pawnlike)) +
  geom_jitter() +
  facet_wrap(~vignette_election) +
  geom_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Competence",
       y = "Pawn-Like")

ggsave("vign_corr_comp_pawn_voters.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 4.7, units = "in", dpi =300)
